/*
 **************************************************************************
 * @author Armen
 * Made:Dec 10, 2009
 * 
 *  
 ************************************************************************** 
 */
package onlinecalsys.server.objects;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalsysCalendar implements Serializable {
	
	private int year;
	private int month;
	private int day;
	private int hour;
	
	public CalsysCalendar(int year, int month, int day, int hour) {
		this.year = year;
		this.month  = month;
		this.day = day;
		this.hour = hour;
	}	
	

	public int getDay() {
		// TODO Auto-generated method stub
		return day;
	}
	
	/**
	 * Checks if is valid date.
	 * 
	 * 
	 * @return true, if is valid date
	 */
	public boolean isValidDate() {
		
		//checking if hour is valid
		if(hour < 0 && hour > 23) {
			return false;
		}
		
		//checking if month is valid
		if(month > 12 || month < 1 ) {
			return false;
		}
		//special checking if the year is a leapyear
		if(month == 2) {
			boolean isLeapYear = leapYear(year);
			if(day > 0 && day <= 28 && !isLeapYear) {
				return true;
			} else if(day > 0 && day <=29 && isLeapYear) {
				return true;
			} else {
				return false;
			}
		}
		//checking if day is valid if its is a month of only 30 days first half of calendar
		if(month % 2 == 0 && month < 8) {
			if(day > 0 && day <= 30) {
				return true;
			}
		//checking if day is valid if its is a month of only 31 days
		} else if(month % 2 == 1 && month < 8) {
			if(day > 0 && day <=31) {
				return true;
			}
		//checking if day is valid if its is a month of only 30 days second half of calendar
		} else if(month % 2 == 0 && month > 7) {
			if(day > 0 && day <=31) {
				return true;
			}
		//checking if day is valid if its is a month of only 31 days
		} else if(month % 2 == 1 && month > 7) {
			if(day > 0 && day <= 30) {
				return true;
			}
		}
		
		return false;
		
	}


	private boolean leapYear(int year) {
		boolean isLeapYear;

        // divisible by 4
        isLeapYear = (year % 4 == 0);

        // divisible by 4 and not 100
        isLeapYear = isLeapYear && (year % 100 != 0);

        // divisible by 4 and not 100 unless divisible by 400
        isLeapYear = isLeapYear || (year % 400 == 0);

        return isLeapYear;
	}


	public int getHour() {
		return hour;
	}


	public int getMonth() {
		return month;

}


	public int getYear() {
		return year;
	}

	
	/**
	 * Today.
	 * Prints of the todays date in format dd/MM/yyyy
	 * @return the string
	 */
	public static String today() {
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		java.util.Date date = new java.util.Date();
		return dateFormat.format(Calendar.getInstance().getTime());
		
	}


	/**
	 * Gets the day before.
	 * Given a date of format dd/MM/yyyy if will return the date of tomorrow with the same format
	 * @param today of type String
	 * 
	 * @return the day before
	 * 
	 * @throws ParseException the parse exception
	 */
	public static String getDayBefore(String today) throws ParseException {
		Calendar cal = Calendar.getInstance();
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		Date date = formatter.parse(today);
		
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, -1);
		Date previousDay = cal.getTime();
		
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		return dateFormat.format(previousDay);

		
		
	}


	/**
	 * Gets the next day.
	 * Given a date with format dd/MM/yyyy it will give the date of the previous date
	 * @param today of type String
	 * 
	 * @return the next day
	 * 
	 * @throws ParseException the parse exception
	 */
	public static String getNextDay(String today) throws ParseException {
		Calendar cal = Calendar.getInstance();
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		Date date = formatter.parse(today);
		
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, 1);
		Date previousDay = cal.getTime();
		
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		return dateFormat.format(previousDay);
		
	}
}
